<template>
  <div class="goods">
    <div class="wrap">
      <Crumb :navs="nav"> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == indexed }"
            v-for="(item, index) in options1"
            :key="index"
            @click="fn(item.min, item.max, index)"
            >{{ item.txt }}</span
          >
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: index == indexed1 }"
            v-for="(item, index) in options2"
            :key="index"
            @click="fn2(item.type, index)"
            >{{ item.txt }}</span
          >
        </li>
      </ul>
      <List :arr="dataShow" />
      <!-- <div>{{ isReachBottom ? "加载完成！" : "加载中。。。" }}</div> -->
      <div class="aaa">
        <i v-if="!isReachBottom" class="iconfont icon-loading"></i>
        <span v-if="isReachBottom">加载完成！</span>
      </div>
    </div>
    <el-backtop :visibility-height="300"></el-backtop>
  </div>
</template>                            
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { ProductsAIP } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  data() {
    return {
      nav: [{ name: "首页" }, { name: "全部商品" }],
      data: [],
      dataShow: [],
      did: 0,
      type: 0,
      min: 0,
      max: 0,
      keyword: "",
      indexed: 0,
      indexed1: 0,
      // 页码
      pag: 1,
      // 渲染条数
      size: 8,
      // 节流开关
      isLoading: false,
      // 判断数据是否加载完毕
      isReachBottom: false,

      options1: [
        { min: 0, max: 0, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options2: [
        { txt: "全部", type: 0 },
        { txt: "实物礼品", type: 1 },
        { txt: "虚拟礼品", type: 2 },
      ],
    };
  },
  components: {
    Crumb,
    List,
  },

  mounted() {
    window.addEventListener("scroll", this.scrollFn);
  },

  created() {
    this.keyword = this.$route.query.keyword || "";

    // 产品数据请求
    this.getProductsAIP();
  },

  beforeDestroy() {
    window.removeEventListener("scroll", this.scrollFn);
  },
  methods: {
    // 产品数据请求
    async getProductsAIP() {
      // 初始化
      this.pag = 1;
      let res = await ProductsAIP({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      if (!res) return;
      this.data = res.data;

      this.dataShow = this.data.filter((item, index) => index < this.size);

      this.isReachBottom = false;
      if (this.dataShow.length == this.data.length) {
        console.log("数据加载完啦！");
        this.isReachBottom = true;
        return;
      }
    },
    // 产品筛选
    fn(min, max, index) {
      this.min = min;
      this.max = max;
      this.indexed = index;
      // 产品数据请求
      this.getProductsAIP();
    },
    fn2(type, index) {
      this.indexed1 = index;
      this.type = type;
      this.getProductsAIP();
    },
    scrollFn() {
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 100) {
        console.log("可加载。。");
        // 判断是否加载完成
        if (this.dataShow.length == this.data.length) {
          console.log("数据加载完啦！");
          this.isReachBottom = true;
          return;
        }

        if (!this.isLoading) {
          this.isLoading = true;
          // 下一页
          this.pag++;
          setTimeout(() => {
            for (
              let i = (this.pag - 1) * this.size;
              i < this.pag * this.size;
              i++
            ) {
              // 添加到数据
              this.data[i] && this.dataShow.push(this.data[i]);
              console.log(this.pag);
            }
            this.isLoading = false;
          }, 500);
        }
      }
    },
  },
  // 方法一
  // watch: {
  //   "$route.query.keyword": {
  //     handler(newVal, oldVal) {
  //       this.keyword = newVal;
  //       this.getProductsAIP();
  //     },
  //   },
  // },
  // 方法二
  beforeRouteUpdate(to, from, next) {
    next();
    this.keyword = to.query.keyword;
    this.getProductsAIP();
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
  .aaa {
    text-align: center;

    i {
      display: inline-block;
      font-size: 30px;
      animation: my 1s linear infinite;
      color: pink;
    }
    @keyframes my {
      from {
        transform: rotate(0deg);
      }
      to {
        transform: rotate(360deg);
      }
    }
  }
}
</style>